import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { useTranslation } from "react-i18next";
import useCallBackState from "../../hooks/useCallBackState";
import toast from "../../compoents/toast";
import useSuperLock from "../../hooks/useSuperLock";
import { authLogin } from "../../api/auth";
import { getUserInfo } from "../../api/user";
import { findAccount } from "../../api/wallet";
import { getMyInviter } from "../../api/user";
import { EVENT, KEYCONST } from "../../enums";
import { useDispatch } from "react-redux";
import useServices from "../../hooks/useRequest";
import kingsInstance from "../../instance";
import { useLocalStorageSetState } from "../../hooks";
import utilsBase from "../../utils/basc";
import useRoute from "../../hooks/useRouter";
import { LocalKeyMap } from "../../local";
export function ViewModel() {
    var handleSwitch = function handleSwitch() {
        setState({
            isEmail: !state.isEmail,
            emailError: false
        });
    };
    var forgetPwd = function forgetPwd() {
        dispatch.app.PUSH({
            path: "/forgetpass"
        });
    };
    var handleSignUp = function handleSignUp() {
        dispatch.app.PUSH({
            path: "/register"
        });
    };
    var _localStore_prevAccount, _localStore_prevAccount1, _localStore_prevAccount2;
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), localStore = _useLocalStorageSetState[0], setLocalStore = _useLocalStorageSetState[1];
    var _useRoute = useRoute(), search = _useRoute.location.search;
    /** write your js */ var _useCallBackState = _sliced_to_array(useCallBackState({
        isEmail: search && search === "phone" ? false : true,
        email: (localStore === null || localStore === void 0 ? void 0 : localStore.prevAccount) && (localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount = localStore.prevAccount) === null || _localStore_prevAccount === void 0 ? void 0 : _localStore_prevAccount.isEmail) ? localStore.prevAccount.account || "" : "",
        area: (localStore === null || localStore === void 0 ? void 0 : localStore.prevArea) || "+1",
        phone: (localStore === null || localStore === void 0 ? void 0 : localStore.prevAccount) && !(localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount1 = localStore.prevAccount) === null || _localStore_prevAccount1 === void 0 ? void 0 : _localStore_prevAccount1.isEmail) ? (localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount2 = localStore.prevAccount) === null || _localStore_prevAccount2 === void 0 ? void 0 : _localStore_prevAccount2.account) || "" : "",
        password: "",
        emailError: false,
        code: ""
    }), 2), state = _useCallBackState[0], setState = _useCallBackState[1];
    var dispatch = useDispatch();
    var services = useServices({
        findAccount: findAccount,
        authLogin: authLogin,
        getUserInfo: getUserInfo,
        getMyInviter: getMyInviter
    });
    var t = useTranslation().t;
    var _useSuperLock = _sliced_to_array(useSuperLock(/*#__PURE__*/ _async_to_generator(function() {
        var data;
        return _ts_generator(this, function(_state) {
            data = {
                // password: state.password,
                email: undefined,
                areaCode: undefined,
                loginName: undefined,
                code: state.code,
                onaAppId: kingsInstance.authConfig.ONA_APPID,
                onaAppSecret: kingsInstance.authConfig.ONA_APP_SECRET
            };
            if (state.isEmail) {
                if (!utilsBase.checkEmail(state.email)) {
                    setState({
                        emailError: false
                    });
                    toast.createToast({
                        type: "errToast",
                        message: t(LocalKeyMap["toast.error.validEmail"])
                    });
                    return [
                        2
                    ];
                }
                data.loginName = state.email;
            } else {
                data.loginName = "".concat(state.area, "-").concat(state.phone);
            }
            services.authLogin(data).then(function(res) {
                if (res.code == 200) {
                    localStorage.setItem(KEYCONST.token, JSON.stringify({
                        accessToken: res.data.token
                    }));
                    dispatch.app.setCoinInfos().then(function() {
                        Promise.all([
                            services.getUserInfo(),
                            services.getMyInviter()
                        ]).then(function(param) {
                            var _param = _sliced_to_array(param, 2), userData = _param[0], inviter = _param[1];
                            if (userData.code !== 200) {
                                toast.createToast({
                                    message: userData.msg,
                                    type: "errToast"
                                });
                                return;
                            }
                            // debugger;
                            localStorage.setItem(KEYCONST.userInfo, JSON.stringify(userData.data));
                            dispatch.app.SET_USER_INFO(userData.data);
                            dispatch.app.SET_INVITER(inviter.data);
                            /** 保存上册登录账号 */ setLocalStore({
                                prevAccount: {
                                    account: state.isEmail ? state.email : state.phone,
                                    isEmail: state.isEmail,
                                    type: state.isEmail ? "mail" : "phone",
                                    nickname: userData.data.nickname
                                },
                                prevArea: state.area
                            });
                            kingsInstance.emit(EVENT.connect, _object_spread({}, res.data));
                            if (res.data.ifFirst) {
                                dispatch.app.PUSH({
                                    path: "/referralCode"
                                });
                            } else {
                                dispatch.app.PUSH({
                                    path: "/home"
                                });
                            }
                        }).catch(function(err) {
                            console.log("error", err);
                        });
                    });
                } else {
                    toast.createToast({
                        message: res.msg,
                        type: "errToast"
                    });
                }
            }).catch(function() {
                setState({
                    password: ""
                });
            });
            return [
                2
            ];
        });
    })), 2), handleSignIn = _useSuperLock[0], loading = _useSuperLock[1];
    return {
        state: state,
        setState: setState,
        handleSwitch: handleSwitch,
        forgetPwd: forgetPwd,
        handleSignIn: handleSignIn,
        handleSignUp: handleSignUp,
        loading: loading
    };
}

 //# sourceMappingURL=view-model.js.map